package sms_interface;


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
 * 
 * @author aayushjain
 * This class contains methods for sending sms to the user once electricity wastage is detected.
 */
public class Sending_sms_mysql {
	/**
	 * When the electricity wastage is detected, this method is called. Using this sms is sent to the concerned authority.
	 * @param room_id room in which wastage is detected
	 * @param Appliance_id Appliance that is wasting electricity.
	 * @return 1 if everything works fine.
	 * @throws Exception
	 */
	static public int get_authority(String room_id,String Appliance_id) throws Exception
    {
            //while(true)
            //{
		String Url = "jdbc:mysql://localhost:3306/Authority_Database";
		String query = "Select * FROM authorities";
		String query2;
		//try {

		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection (Url,"root","aayush");
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery(query);
		
		while (rs.next()) {
			int aid=rs.getInt(1);
			String Authority_Name=rs.getString(2);
            String number="0"+rs.getString(3);
            String message;
            //System.out.println(arr1[0]);
            if( room_id.equalsIgnoreCase(rs.getString(4))==true)
            {
            message="Electricity wastage detected in room no. "+room_id+" and ApplianceID:"+Appliance_id+". Being the concerned authority("+Authority_Name+"), please switch off the appliance ASAP.";
            System.out.println(message.length());
            //message="hello world";
            System.out.println(message);
            send_sms(number,message);
            //System.out.println(now());
            //date();
            query2="Insert into log_sms values('"+Authority_Name+"','"+date()+"','"+now()+"','"+message+"','"+Appliance_id+"')";
            //System.out.println(query2);
            stmt.executeUpdate(query2);
            break;
            } 
		} //end while

		con.close();
		return 1;
			//}
    }
    
	
	
	
	/**
	 * Code to send sms using the gsm gateway. This code has been taken from google repository IIIT-D ivrs.
	 * @param number number to which sms to be sent.
	 * @param message message to be sent to the user.
	 * @throws InterruptedException
	 */
	public static void send_sms(String number, String message) throws InterruptedException
    {
            String arr[] =message.split("\\s");
            String reformed ="";
            for(int i = 0;i<arr.length;i++)
            {
                    reformed = reformed +"%20" + arr[i];
            }
            System.out.println(reformed);
            System.out.println("Query formed:\nwget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number="+number.toString()+"&smsencode="+reformed+"&send=Send");
            /*try {
                    Runtime.getRuntime().exec("wget -o smssent -r --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-send.html?number="+number.toString()+"&smsencode="+reformed+"&send=Send").waitFor();
            } catch (IOException e) {
                    
                    e.printStackTrace();
            }*/
    }
	
	
	/**
	 * Returns the time at the current moment.
	 * Ref:http://stackoverflow.com/questions/833768/java-code-for-getting-current-time
	 * @return Time in String.
	 */
    static public String now()
    {
    Calendar cal = Calendar.getInstance();
    cal.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    return (String)sdf.format(cal.getTime());

    }
    
    /**
     * Returns the date at the moment.
     * @return date at the time of calling this function.
     */
    static public String date(){
    	 Calendar cal = new GregorianCalendar();
    	  int month = cal.get(Calendar.MONTH);
    	  int year = cal.get(Calendar.YEAR);
    	  int day = cal.get(Calendar.DAY_OF_MONTH);
    	  return(day + ":" + (month + 1) + ":" + year);
    	  
    }
    /*static public void main(String[] args) throws Exception
    {
            get_authority("CR2","F2");
            //get_authority("CR2","A2");
            //get_authority("CR1","B2");
            
    }*/

}
